package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.Client;
import com.emex.app.stuff.provider.ClientDataProvider;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey Karavaev
 */
public class StaffTableModel extends AbstractTableModel implements TableModel, TableCrud<Client> {

    /**
     * List of all clients.
     */
    private List<Client> clients;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Фамилия", "Имя", "Отчество", "Телефон", "E-mail", "Интернет-пользователь"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class, String.class, String.class, String.class, String.class, String.class
    };
    /**
     * Users count.
     */
    private int usersCount = 0;
    /**
     * Internet users count.
     */
    private int internetUsersCount = 0;

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public StaffTableModel() {
        clients = ClientDataProvider.getInstance().getAllClients();
        initializeUsersCount();
    }

    public StaffTableModel(List<Client> clientsList) {
        clients = clientsList;
        initializeUsersCount();
    }

    private void initializeUsersCount() {
        if (null != clients && !clients.isEmpty()) {
            for (Client client : clients) {
                if (client.getIsInternetUser()) {
                    ++internetUsersCount;
                } else {
                    ++usersCount;
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return clients.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if ((rowIndex >= 0) && (rowIndex < clients.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return clients.get(rowIndex);
                }
                case 0: {
                    return clients.get(rowIndex).getSurname();
                }
                case 1: {
                    return clients.get(rowIndex).getName();
                }
                case 2: {
                    return clients.get(rowIndex).getPatronymic();
                }
                case 3: {
                    return clients.get(rowIndex).getPhones();
                }
                case 4: {
                    return clients.get(rowIndex).getEmail();
                }
                case 5: {
                    if (clients.get(rowIndex).getIsInternetUser()) {
                        return "Да";
                    } else {
                        return "Нет";
                    }
                }

                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(Client t) {
        clients.remove(t);
        if (t.getIsInternetUser()) {
            --internetUsersCount;
        } else {
            --usersCount;
        }
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(Client t) {
        clients.add(t);
        if (t.getIsInternetUser()) {
            ++internetUsersCount;
        } else {
            ++usersCount;
        }
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }

    public int getUsersCount() {
        return usersCount;
    }

    public int getInternetUsersCount() {
        return internetUsersCount;
    }
}
